package hos.filex.delegate;

import android.net.Uri;

import androidx.activity.ComponentActivity;
import androidx.activity.result.ActivityResultCallback;
import androidx.activity.result.ActivityResultLauncher;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.LifecycleOwner;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import hos.file.media.FileType;
import hos.file.provider.FileProviderX;

/**
 * <p>Title: ImageDelegate </p>
 * <p>Description:  </p>
 * <p>Company: www.mapuni.com </p>
 *
 * @author : 蔡俊峰
 * @version : 1.0
 * @date : 2024-03-14 15:03
 */
public class CameraDelegate extends CameraImageDelegate {

    @NonNull
    public static CameraDelegate create(ComponentActivity activity) {
        return new CameraDelegate(activity);
    }

    @NonNull
    public static CameraDelegate create(Fragment fragment) {
        return new CameraDelegate(fragment);
    }

    protected CameraDelegate(@NonNull LifecycleOwner lifecycleOwner) {
        super(lifecycleOwner);
    }

    @Override
    public void onCreate(@NonNull LifecycleOwner owner) {
        super.onCreate(owner);
        createCameraAudio();
        createCameraVideo();
    }

    @Nullable
    private ActivityResultLauncher<String> createCameraAudioLauncher;

    @NonNull
    private ActivityResultLauncher<String> createCameraAudio() {
        if (createCameraAudioLauncher != null) {
            return createCameraAudioLauncher;
        }
        return createCameraAudioLauncher = FileResultContracts.createCameraAudio(requireActivityResultCaller(), new ActivityResultCallback<Uri>() {
            @Override
            public void onActivityResult(Uri uri) {
                handleUri(uri);
            }
        });
    }

    @Nullable
    private ActivityResultLauncher<String> createCameraVideoLauncher;

    @NonNull
    private ActivityResultLauncher<String> createCameraVideo() {
        if (createCameraVideoLauncher != null) {
            return createCameraVideoLauncher;
        }
        return createCameraVideoLauncher = FileResultContracts.createCameraVideo(requireActivityResultCaller(), new ActivityResultCallback<Uri>() {
            @Override
            public void onActivityResult(Uri uri) {
                handleUri(uri);
            }
        });
    }

    //<editor-fold desc="Uri">
    /**
     * 处理Uri
     */
    protected void handleUri(Uri uri) {
        if (uri == null) {
            if (mOnResultListener != null) {
                mOnResultListener.onResult(null);
            }
            return;
        }
        File file = FileProviderX.uri2File(requireActivity(), uri);
        if (file != null) {
            if (FileType.isIMAGE(file.getAbsolutePath())) {
                handleImageUri(uri);
                return;
            }
        }
        if (mOnResultListener != null) {
            if (file == null) {
                mOnResultListener.onResult(null);
                return;
            }
            mOnResultListener.onResult(file.getAbsolutePath());
        }
    }

    /**
     * 处理Uri
     */
    protected void handleUriList(List<Uri> uriList) {
        if (uriList == null || uriList.isEmpty()) {
            if (mOnResultListListener != null) {
                mOnResultListListener.onResult(null);
            }
            return;
        }
        new Thread(() -> {
            List<String> pathList = new ArrayList<>();
            for (Uri uri : uriList) {
                File file = FileProviderX.uri2File(requireActivity(), uri);
                if (file != null) {
                    if (FileType.isIMAGE(file.getAbsolutePath())) {
                        // 如果是图片的话，就需要进行压缩判断
                        File zipImageFile = zipImageFile(file);
                        if (zipImageFile != null) {
                            pathList.add(zipImageFile.getAbsolutePath());
                        }
                        continue;
                    }
                    pathList.add(file.getAbsolutePath());
                }
            }
            runMain(() -> {
                if (mOnResultListListener != null) {
                    if (pathList.isEmpty()) {
                        mOnResultListListener.onResult(null);
                        return;
                    }
                    mOnResultListListener.onResult(pathList);
                }
            });
        }).start();
    }
    //</editor-fold>

    //<editor-fold desc="CameraAudio">
    public void createCameraAudio(FileResult.OnResultListener onResultListener) {
        createCameraAudio(null, onResultListener);
    }

    public void createCameraAudio(String audioName, FileResult.OnResultListener listener) {
        mOnResultListener = listener;
        try {
            createCameraAudio().launch(audioName);
        } catch (Exception e) {
            e.printStackTrace();
            if (listener != null) {
                listener.onResult(null);
            }
        }
    }
    //</editor-fold>

    //<editor-fold desc="CameraVideo">
    public void createCameraVideo(FileResult.OnResultListener onResultListener) {
        createCameraVideo(null, onResultListener);
    }

    public void createCameraVideo(String audioName, FileResult.OnResultListener listener) {
        mOnResultListener = listener;
        try {
            createCameraVideo().launch(audioName);
        } catch (Exception e) {
            e.printStackTrace();
            if (listener != null) {
                listener.onResult(null);
            }
        }
    }
    //</editor-fold>


    @Override
    public void onDestroy(@NonNull LifecycleOwner owner) {
        mOnResultListener = null;
        mOnResultListListener = null;
        createCameraAudioLauncher = null;
        createCameraVideoLauncher = null;
        super.onDestroy(owner);
    }

}
